﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Catering.yl.Daily
{
    public partial class CustomerPropertyList : System.Web.UI.Page
    {
        static string className = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if(!Page.IsPostBack)
            {
                className = Session["ClassName"].ToString();
                LoadForm();
            }            
        }
        public void LoadForm()
        {
            try
            {
                DataTable dt_Plan = new Catering_Rules.CustomerPropertyList().GetList("").Tables[0];

                for (int i = 0; i < dt_Plan.Rows.Count; i++ )
                {
                    for (int j = 0; j < dt_Plan.Columns.Count; j++ )
                    {
                        if (string.IsNullOrEmpty(dt_Plan.Rows[i][j].ToString()))
                            dt_Plan.Rows[i][j] = "&nbsp"; 
                    }                    
                }
                dt_Plan.Columns.Add("tempId");
                for (int i = 0; i < dt_Plan.Rows.Count; i++)
                {
                    dt_Plan.Rows[i]["tempId"] = i + 1;
                }
                this.Rep_Plan.DataSource = dt_Plan;
                this.Rep_Plan.DataBind();
            }
            catch
            {

            }
        }
        [WebMethod]
        public static string Add(string Company, string CustomerName, string Contacts, string Phone)
        {
            Catering_Model.CustomerPropertyList _cp = new Catering_Model.CustomerPropertyList();
            _cp.SNumber = DateTime.Now.ToString("yyyyMMddHHmmss");
            _cp.Company = Company;
            _cp.CustomerName = CustomerName;
            _cp.Contacts = Contacts;
            _cp.Phone = Phone;
            _cp.LPerson = "";
            _cp.ClassName = className;
            _cp.SaveDate = Convert.ToDateTime(DateTime.Today.ToString("yyyy-MM-dd"));

            new Catering_Rules.CustomerPropertyList().Add(_cp);
            return "OK";
        }       
        [WebMethod]
        public static ArrayList Get(string _id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.CustomerPropertyList md = new Catering_Rules.CustomerPropertyList().GetModel(Convert.ToInt32(_id));
            al.Add(md.Id);
            al.Add(md.Company);
            al.Add(md.CustomerName);
            al.Add(md.Contacts);
            al.Add(md.Phone);            
            return al;
        }
        [WebMethod]
        public static string Edit(string _id, string Company, string CustomerName, string Contacts, string Phone)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(_id, out id);
            if (id <= 0) return "NO";

            Catering_Model.CustomerPropertyList _CE = new Catering_Model.CustomerPropertyList();

            _CE.Id = id;
            _CE.Company = Company;
            _CE.CustomerName = CustomerName;
            _CE.Contacts = Contacts;
            _CE.Phone = Phone;
            new Catering_Rules.CustomerPropertyList().Update(_CE);

            return "OK";
        }
        protected void Rep_Plan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.CustomerPropertyList().Delete(delID))
                {
                    this.LoadForm();
                }
            }
        }
    }
}